package com.itheima.web.controller;

import com.itheima.Vo.ResultInfo;
import com.itheima.domain.Student;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @GetMapping()
    public ResultInfo findAll() {
        List<Student> studentList = studentService.findAll();
        return ResultInfo.success(studentList);
    }

    @PostMapping()
    public ResultInfo save(@RequestBody Student student) {
        studentService.save(student);
        return ResultInfo.success(null);
    }

    @GetMapping("/{number}")
    public ResultInfo findByNumber(@PathVariable("number") String number) {
        Student student = studentService.findByNumber(number);
        return ResultInfo.success(student);
    }

    @PutMapping()
    public ResultInfo update(@RequestBody Student student) {
        studentService.update(student);
        return ResultInfo.success(null);
    }

    @DeleteMapping("/{number}")
    public ResultInfo deleteByNumber(@PathVariable("number") String number) {
        studentService.deleteByNumber(number);
        return ResultInfo.success(null);
    }
}
